// pages/gjIndex/msgList/msgList.js
import config from "../../../config.js";
const base = config;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msgList: [],
    type: 1
  },

  makeCall: function(e) {
    let tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      type: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.addListener();

    this.loadData();

  },
  addListener: function() {
    var that = this;
    console.log("添加监听");
    app.globalData.callback = function(res) {
      console.log('收到服务器内容2：' + res.data)
      wx.setTabBarBadge({
        'index': 2,
        text: '1'
      })
      that.loadData();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  contactAction: function(e) {
    console.log(e);
    wx.setStorageSync("chatUser", e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '../../msg/chat',
    })
  },
  loadData: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: base.chatMessageList,
      data: {
        start: 0,
        size: 10,
        from: that.data.type
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            msgList: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }
      }
    })
  }
})